/**
 * 投放控制列表
 */
define([
        '../../common/service',
        './feed',
        '../../../app/framework/bean/productSet'
    ],
    function(
        http,
        feed,
        productSetModel
    ) {
        var ProductPreview = {
            $table: $(".ndp-table-wrapper[name='solid-split-table']"),
            init: function() {
                var _this = this;
                feed._init_tabs(2);
                _this.weFeedId = window.localStorage.getItem('weFeedId');
                _this.productSetId = Kit.getUrlParam("setId");
                if (_this.productSetId) {
                    _this.initTable();
                } else if (_this.weFeedId) {
                    _this.init_table(_this.weFeedId);
                }

            },
            init_table: function(weFeedId) {
                var _this = this;
                //从sessionstorage中获取feedId
                // todo
                // feedId:window.sessionStorage.getItem("feedId");
                http.do('/productview/originalShow ', { feedId: weFeedId }, 'POST').then(function(res) {
                    if (res.flag == 'fail') {
                        Kit.alert2(res.errorMsg);
                        return;
                    }
                    _this.renderTable(res.data);
                });
            },
            renderTable: function(results) {
                var _this = this;
                var tableData = [];
                $.each(results, function(i, obj) {
                    //处理渲染的数据
                    var image_linkDom = "<img class='img-view' src='" + obj.image_link + "' />";
                    var linkDom = "<a target='_blank' href='" + obj.link + "'>" + obj.link + "</a>";
                    var tempObj = {
                        id: obj.id,
                        title: obj.title,
                        image_link: {
                            txt:image_linkDom,
                            title: obj.title,
                            val:obj.image_link
                        },
                        link: {
                            title: obj.title,
                            txt:linkDom,
                            val:obj.link
                        },
                        brand: obj.brand,
                        price: obj.price,
                        availability: obj.availability
                    };
                    tableData.push(tempObj);
                });
                var ta = $(".ndp-table-wrapper[name='solid-split-table']").table({
                    head: {
                        col1: I18N.getText('feedList', 'productNO'),
                        col2: I18N.getText('feedList', 'name'),
                        col3: I18N.getText('feedList', 'image'),
                        col4: I18N.getText('feedList', 'landingPage'),
                        col5: I18N.getText('feedList', 'brand'),
                        col6: I18N.getText('feedList', 'price'),
                        col7: I18N.getText('feedList', 'availability')
                    },
                    data: tableData
                }).on("ROW_CLICK", function(e) { //行被点击
                    _this.bind_table_row(_this.$table,e);
                });
                ta.find('li.ndp-table-row span a[target="_blank"]').off('click').on('click',function(e) {
                    e.stopPropagation();
                });
            },
            bind_table_row: function(tableObj,e) {
                var _this = this;
                var itemId = $(e.originalEvent.target).data('id');
                //显示 dialog
                _this.show_dialog(itemId);
            },
            show_dialog: function(itemId) {
                var _this = this;
                $('#detail-modal').modal({ backdrop: 'static', keyboard: false });
                _this.get_table_data(itemId);
            },
            get_table_data: function(itemId) {
                var _this = this;
                http.do('/productview/originalShowItem ', { feedId: _this.weFeedId, itemId: itemId }, 'POST').then(function(res) {
                    if (res.flag == 'fail') {
                        //Kit.loading.hide(loadingArea);
                        Kit.alert2(res.errorMsg);
                        return;
                    }
                    var feedObj = res.data;
                    //构造表格
                    var html = "";
                    html += _this.make_table_body("id", feedObj.id, feedObj.image_link);
                    html += _this.make_table_body("age_group", feedObj.age_group);
                    html += _this.make_table_body("availability", feedObj.availability);
                    html += _this.make_table_body("brand", feedObj.brand);
                    html += _this.make_table_body("category", feedObj.category);
                    html += _this.make_table_body("condition", feedObj.condition);
                    html += _this.make_table_body("description", feedObj.description);
                    html += _this.make_table_body("gender", feedObj.gender);
                    html += _this.make_table_body("title", feedObj.title);
                    html += _this.make_table_body("price", feedObj.price);
                    html += _this.make_table_body("product_type", feedObj.product_type);
                    html += _this.make_table_body("link", feedObj.link);
                    $("#detail-modal").find("tbody").html(html);
                });
            },
            make_table_body: function(col1, col2, col3) {
                var html = "<tr>";
                if (typeof col3 != "undefined") {
                    html += "<td class='img-wrapper' rowspan='13'><div><img src='" + col3 + "'/></div></td>";
                }
                html += "<td width='80px'>" + col1 + "</td>";
                html += "<td>" + ((typeof(col2) === "undefined") ? '' : col2) + "</td>";
                html += "</tr>";
                return html;
            },
            initTable: function() {
                var _this = this;
                var params = {
                    product_set_id: _this.productSetId
                }
                productSetModel.preview(params, function(results) {
                    _this.renderTable(results);
                });
            }
        }
        return ProductPreview;
    });
